###-*- R -*-

## this will break if R is on a network share
.Library <- file.path(chartr("\\", "/", R.home()), "library")
.Library.site <- Sys.getenv("R_LIBS_SITE")
.Library.site <- if(!nchar(.Library.site)) file.path(R.home(), "site-library") else unlist(strsplit(.Library.site, ";"))
.Library.site <- .Library.site[file.exists(.Library.site)]

if(!nzchar(Sys.getenv("R_LIBS_USER")))
    Sys.setenv(R_LIBS_USER=
               file.path(Sys.getenv("R_USER"), "R",
                         "win-library",
                         paste(R.version$major,
                               sub("\\..*$", "", R.version$minor),
                               sep=".")
                         ))

invisible(.libPaths(c(unlist(strsplit(Sys.getenv("R_LIBS"), ";")),
                      unlist(strsplit(Sys.getenv("R_LIBS_USER"), ";"))
                      )))

if(nzchar(Sys.getenv("R_PAPERSIZE"))) {
    options(papersize = as.vector(Sys.getenv("R_PAPERSIZE")))
} else {
    if(length(grep("united.states", Sys.getlocale("LC_MONETARY"), TRUE)) ||
       length(grep("canada", Sys.getlocale("LC_MONETARY"), TRUE)))
        options(papersize = "letter")
    else options(papersize = "a4")
}

options(pager = if(length(grep("--ess", commandArgs()))) "console" else "internal")
options(useFancyQuotes = (.Platform$GUI == "Rgui"))
options(pdfviewer = file.path(R.home(), "bin", "open.exe"))

if(.Platform$GUI == "Rgui") {
    Sys.setenv(GFORTRAN_STDOUT_UNIT= "-1")
    Sys.setenv(GFORTRAN_STDERR_UNIT= "-1")
}

local({
    tests_startup <- Sys.getenv("R_TESTS")
    if(nzchar(tests_startup)) source(tests_startup)
})
